﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Data;

namespace Web.manager
{
    public partial class groupedit : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                DataSet ds = PhoneBook.Industry.Industry.list();
                Bind_ddl_industry(ds);

                Hashtable group = PhoneBook.Group.Group.get(Commons.StrUtil.FormatValue(Request["groupid"]));
                Hashtable industry = PhoneBook.Industry.Industry.get(Commons.StrUtil.FormatValue(group["industryid"]));
                Helper.Model2ViewState(group, ViewState);
                Helper.Model2ViewState(industry, ViewState);

                if (ddl_industry.Items.FindByValue(Commons.StrUtil.FormatValue(group["industryid"])) != null)
                    ddl_industry.SelectedValue = Commons.StrUtil.FormatValue(group["industryid"]);

                txb_groupname.Text = Commons.StrUtil.FormatValue(group["groupname"]);
                txb_totalnum.Text = Commons.StrUtil.FormatValue(group["totalnum"]);
                txb_usenum.Text = Commons.StrUtil.FormatValue(group["usenum"]);
                txb_regaddress.Text = Commons.StrUtil.FormatValue(group["regaddress"]);
                txb_linkaddress.Text = Commons.StrUtil.FormatValue(group["linkaddress"]);
                txb_phone.Text = Commons.StrUtil.FormatValue(group["phone"]);
                txb_fax.Text = Commons.StrUtil.FormatValue(group["fax"]);
            }
        }

        protected void Bind_ddl_industry(DataSet ds)
        {
            Commons.DataUtil.DataSort(ds, "isnull(parentid,industryid)");
            Commons.ControlUtil.BindDDL(ddl_industry, ds, "industryname", "industryid");
        }

        protected void btn_save_Click(object sender, EventArgs e)
        {
            Hashtable group = PhoneBook.Group.Group.get(Commons.StrUtil.FormatValue(Request["groupid"].ToString()));
            addlog(group);

            group["groupid"] = Request["groupid"].ToString();
            group["groupname"] = txb_groupname.Text;
            group["totalnum"] = txb_totalnum.Text;
            group["usenum"] = txb_usenum.Text;
            group["regaddress"] = txb_regaddress.Text;
            group["linkaddress"] = txb_linkaddress.Text;
            group["phone"] = txb_phone.Text;
            group["fax"] = txb_fax.Text;

            ERMap.ERMapHelper.setmodel("b_group", "groupid", group);
            
            Helper.ShowMessage(u_message, controls.UCMessageType.success, "保存成功");
        }

        public void addlog(Hashtable group)
        {
            string content = string.Empty;
            if (group["groupname"].ToString() != txb_groupname.Text)
                content += string.Format("名称:{0}->{1}", group["groupname"], txb_groupname.Text);

            if (group["regaddress"].ToString() != txb_regaddress.Text)
                content += string.Format("注册地址:{0}->{1}", group["regaddress"], txb_regaddress.Text);

            if (group["linkaddress"].ToString() != txb_linkaddress.Text)
                content += string.Format("联系地址:{0}->{1}", group["linkaddress"], txb_linkaddress.Text);

            if (group["phone"].ToString() != txb_phone.Text)
                content += string.Format("电话:{0}->{1}", group["phone"], txb_phone.Text);

            if (group["fax"].ToString() != txb_fax.Text)
                content += string.Format("传真:{0}->{1}", group["fax"], txb_fax.Text);

            if (!string.IsNullOrEmpty(content))
            {
                content = string.Format("修改了集团信息({0})。{1}", group["groupid"], content);

                AddLog(content);
            }
        }
    }
}